﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Xml.Serialization;

namespace Eugenhome.Atrium.Rss
{
    /// <summary>
    /// 
    /// </summary>
    [XmlInclude(typeof(RssSource))]
    public class RssSourceCollection
    {
        /// <summary></summary>
        public List<RssSource> RssSources { get; set; }

        /// <summary></summary>
        private static string _fullFileName = "rss.xml";// Path.GetFullPath("rss.xml");

        /// <summary></summary>
        public string ProxyAddress { get; set; }

        /// <summary></summary>
        public List<string> ProxyAddresses = new List<string>();

        /// <summary></summary>
        public string TempFolderPath
        {
            get
            {
                string temppath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "temp");
                if (!Directory.Exists(temppath))
                    Directory.CreateDirectory(temppath);
                return temppath;
            }
        }

        /// <summary></summary>
        private string _selectedLabel;

        /// <summary></summary>
        public string SelectedLabel
        {
            get
            {
                if (_selectedLabel == null)
                {
                    SelectedLabel = RssSources[0].Name;
                }

                return _selectedLabel;
            }
            set
            {
                _selectedLabel = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public RssSourceCollection()
        {
            RssSources = new List<RssSource>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="selectedLabel"></param>
        public void SetSelectedLabel(string selectedLabel)
        {
            SelectedLabel = selectedLabel;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool Save()
        {
            bool result = true;
            XmlSerializer serializier = new XmlSerializer(this.GetType());

            if (File.Exists(_fullFileName))
            {
                string bakfile = Path.Combine(TempFolderPath, Path.GetFileNameWithoutExtension(_fullFileName) + ".bak");
                if (File.Exists(bakfile)) File.Delete(bakfile);

                File.Move(_fullFileName, bakfile);
            }

            TextWriter writer = new StreamWriter(_fullFileName, false, Encoding.UTF8);
            try
            {
                serializier.Serialize(writer, this);
                writer.Close();
            }
            catch
            {
                result = false;
                writer.Close();

            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        public void ClearTempFolder()
        {
            foreach (string tempfile in Directory.GetFiles(TempFolderPath, "*.htm"))
            {
                File.Delete(tempfile);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fullFileName"></param>
        /// <returns></returns>
        public static RssSourceCollection Load(string fullFileName)
        {
            _fullFileName = fullFileName;

            return Load();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        internal static RssSourceCollection Load()
        {
            RssSourceCollection result = null;

            if (!File.Exists(_fullFileName))
            {
                result = new RssSourceCollection();
                result.Save();
            }

            result = Read(_fullFileName);

            if (result == null)
            {
                result = new RssSourceCollection();
                result.Save();

                result = Read(_fullFileName);
            }

            result.RssSources.ForEach(item => item.Init(result, result.ProxyAddress));

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        private static RssSourceCollection Read(string filename)
        {
            RssSourceCollection result = null;
            TextReader reader = new StreamReader(filename);
            XmlSerializer serializier = new XmlSerializer(typeof(RssSourceCollection));
            try
            {
                result = (RssSourceCollection)serializier.Deserialize(reader);
                reader.Close();
            }
            catch
            {
                reader.Close();
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="rssurl"></param>
        public void Add(string name, string rssurl)
        {
            int maxID = RssSources.Max(item => item.ID);
            RssSource rss = new RssSource(++maxID, rssurl, name, ProxyAddress);
            RssSources.Add(rss);
            Save();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="rss"></param>
        public void Delete(RssSource rss)
        {
            RssSources.Remove(rss);
            Save();
        }
    }
}
